export default {
    webSocket: null,
    callBack: null,
    initWebSock(path, callBack) {
        // 初始化WebSocket
        this.webSocket = new WebSocket(path);
        // 设置回调函数
        this.callBack = callBack;
        // 设置websocket的监听
        const _this = this;
        this.webSocket.onopen = (res) => _this.onopen(res);
        this.webSocket.onmessage = (res) => _this.onmessage(res);
        this.webSocket.onclose = (res) => _this.onclose(res);
        this.webSocket.onerror = (res) => _this.onerror(res);
    },
    // 消息发送
    sendMsg(value) {
        this.webSocket.send(value);
    },
    onopen(res) {
        console.log(res);
    },
    onmessage(res) {
        this.callBack(res.data)
    },
    onclose(res) {
        console.log(res);
    },
    onerror(res) {
        console.log(res);
    }
}